(function () {
  new Vue({
    el: '#JBchange',
    data() {
      return {
        id: hlUrlUtil.GetQueryString('id'), //合同ID
        code: hlUrlUtil.GetQueryString('code'), //变更code
        condition: hlUrlUtil.GetQueryString('condition'), //变更内容
        contractDetail: {
          ZDYdata: [], //自定义信息
          tmpData: [],
          resData: [],
          mediationData: '',
        }, //合同详情



        zlfTypeList: [{
          name: '公司租赁',
          code: 'C'
        }, {
          name: '个人租赁',
          code: 'P'
        }],
        industryList: [],
        companyList: '', //客户列表
        channelType: [], //渠道分类列表
        channelName: [], //渠道名称列表



      }
    },
    methods: {
      // 获取渠道分类
      getChannelType() {
        let that = this
        hlhttp.get({
          url: 'configure/dict',
          data: {
            module_code: 'ZHSH',
            config_name: 'ZS_CHANNEL_CATE'
          },
          success(res) {
            res.map(item => {
              item.name = item.dictName,
                item.code = item.dictCode
            })
            that.channelType = res
          }
        })
      },
      //获取渠道名称
      getChannelName(val, callback) {
        this.contractDetail.channelNameCode = ''
        this.contractDetail.channelNameVal = ''
        hlhttp.get({
          url: 'configure/channel_matching',
          data: {
            channel_category_code: this.contractDetail.channelCategoryCode + '',
            chennel_name: val || '#'
          },
          success(res) {
            res.map(item => {
              item.name = item.dictName
              item.code = item.dictCode
            })
            callback(res)
          }
        })
      },
      // 选择渠道名称
      selectchannelName(ops) {
        this.contractDetail.channelNameCode = ops.dictCode
        this.contractDetail.channelNameVal = ops.dictName
        if (ops.code == '#') {
          this.contractDetail.mediationData = ''
          return
        }
        hlhttp.get({
          url: 'configure/getIntermediaryInfoByDictCode',
          data: {
            dict_code: ops.code
          },
          success: res => {
            this.$set(this.contractDetail, 'mediationData', res)
          }
        })
      },
      //获取客户名称
      getCompany(val, callback) {
        let power = JSON.parse(localStorage.getItem('permissions'))
        if (power.indexOf('c152') == -1 && power.indexOf('c43') == -1) {
          hl.toast('无客户管理权限')
          return
        }
        this.contractDetail.zlfId = ''
        this.contractDetail.zlfCompany = ''
        hlhttp.get({
          url: 'customers/_search',
          data: {
            begin_date: '#', //付款开始日期
            end_date: '#', //付款结束日期
            key_word: val || '#', //关键字
            customer_state: '#', //客户状态
            black_state: '#', //Y黑名单客户，N不是黑名单
            page: 1, //请求页 Int
            page_size: 10000, //每页数量 Int
            rperson_ids: '#',
            columns: '#',
            orders: '#',
            is_myself: power.indexOf('c43') > -1 ? 'N' : 'Y'
          },
          success(res) {
            callback(res.customers)
          }
        })
      },
      // 选择客户Id
      selectCompanyId(val) {
        console.log(val)
        this.contractDetail.zlfId = val.id
        this.contractDetail.zlfCompany = val.name
        this.contractDetail.zlfIndustryType = [val.industryCode]
        this.contractDetail.idcode = val.idcode
        this.contractDetail.email = val.email
        this.contractDetail.zlfSperson = val.lperson
        this.contractDetail.zlfSphone = val.phone
        this.contractDetail.zlfLperson = val.legalPersonName
        this.contractDetail.channelCategoryCode = [val.channelCategoryCode]
        this.contractDetail.channelNameVal = val.channelNameVal
        this.contractDetail.channelNameCode = val.channelNameCode
        this.contractDetail.zlfAddress = val.address
        this.contractDetail.zlfLphone = ''
      },
      //获取行业
      getIndustry() {
        let that = this
        hlhttp.get({
          url: 'configure/industry/dict',
          success(res) {
            res.map(item => {
              item.name = item.dictName,
                item.code = item.dictCode
            })
            that.industryList = res
          }
        })
      },
      // 同步招商数据
      syncData: function () {
        var that = this;
        hlhttp.get({
          url: 'customer,records',
          data: {
            'customer_id': that.contractDetail.zlfId
          },
          success: function (data) {
            that.contractDetail.zlfType = data.type == 'PE' ? ['P'] : ['C']
            that.contractDetail.zlfCompany = data.name
            that.contractDetail.zlfIndustryType = [data.industry]
            that.contractDetail.idcode = data.idcode
            that.contractDetail.email = data.email
            that.contractDetail.zlfSperson = data.lperson
            that.contractDetail.zlfSphone = data.phone
            that.contractDetail.channelCategoryCode = [data.channelCategoryCode]
            that.contractDetail.channelNameVal = data.channelNameVal
            that.contractDetail.channelNameCode = data.channelNameCode
          }
        })
      },
      //检测当前页面信息是否填写完成
      checkComplete() {
        if (!this.contractDetail.zlfCompany || !this.contractDetail.zlfId) {
          hl.toast('请输入客户名称')
          return false
        }
        if (!this.contractDetail.idcode) {
          hl.toast('请输入纳税人识别号')
          return false
        }
        if (!this.contractDetail.idcode) {
          hl.toast('请输入纳税人识别号')
          return false
        }
        if (!this.contractDetail.zlfSperson) {
          hl.toast('请输入联系人')
          return false
        }
        if (!this.contractDetail.zlfSphone) {
          hl.toast('请输入联系人联系方式')
          return false
        }
        // if (!this.contractDetail.zlfAddress) {
        //   hl.toast('请输入通讯地址')
        //   return false
        // }
        return true
      },
      // 检测自定义字段填写是否符合要求
      checkCustom() {
        let flag = true
        this.contractDetail.resData.some(item => {
          if (item.requiredFlag == 'Y' && (!item.value || !item.value.length) && item.type != 'line') {
            flag = false
          }
        })
        return flag
      },
      resMake(type, val) {
        // text  textarea mobile email phone  time date  dateTime
        let t0 = ['text', 'email', 'area', 'mobile', 'phone', 'time', 'date', 'dateTime', 'select', 'radio', 'checkbox'];
        let t1 = ['file'];
        if (t0.indexOf(type) > -1) return val;
        if (t1.indexOf(type) > -1) {
          let data = []
          if (val) {
            val.map(item => {
              data.push(item.response ? item.response : item)
            })
          }
          return data
        }
      },

      newFieldsData() {
        let data = [];
        this.contractDetail.tmpData = []
        this.contractDetail.resData.map(item => {
          data.push({
            component_code: item.code,
            component_type: item.type,
            property: this.resMake(item.type, item.value)
          })
          this.contractDetail.tmpData.push({
            componentCode: item.code,
            componentType: item.type,
            property: ['select', 'radio', 'checkbox', 'file'].indexOf(item.type) > -1 ? JSON.stringify(this.resMake(item.type, item.value)) : this.resMake(item.type, item.value)
          })
        })

        return data;
      },
      // 检测一般信息是否变更
      checkChange() {
        hlhttp.post({
          url: 'contract_history/general_contrast',
          data: {
            "contract_id": this.id, // 原合同id
            "m_code": this.contractDetail.mCode || '#', // 原合同管理编号
            "c_code": this.contractDetail.cCode || '#', //合同编号
            "name": this.contractDetail.name || '#', //合同名称
            "memo": this.contractDetail.memo || '#', //合同备注
            "sign_time": this.contractDetail.sign_time || '#', // 签订时间
            "contract_type": this.contractDetail.contractType || '#', //合同类型
            "zlf_info": JSON.stringify({ //客户信息
              zlf_type: this.contractDetail.zlfType + '',
              zlf_id: this.contractDetail.zlfId + '' || '#',
              zlf_name: this.contractDetail.zlfCompany || '#',
              zlf_lperson: this.contractDetail.zlfLperson || '#',
              zlf_sperson: this.contractDetail.zlfSperson || '#',
              zlf_lphone: this.contractDetail.zlfLphone || '#',
              zlf_sphone: this.contractDetail.zlfSphone || '#',
              zlf_industry_type: this.contractDetail.zlfIndustryType + '' || '#',
              zlf_address: this.contractDetail.zlfAddress || '#',
              short_name: this.contractDetail.shortName || '#',
              idcode: this.contractDetail.idcode || '#',
              email: this.contractDetail.email || '#',
              channel_category_code: this.contractDetail.channelCategoryCode + '' || '#',
              channel_name_code: this.contractDetail.channelNameCode + '' || '#',
              channel_name_value: this.contractDetail.channelNameVal || '#'
            }),
            "custom_config_property": JSON.stringify(this.contractDetail.ZDYdata),
            page_customize_component_biz_content: JSON.stringify(this.newFieldsData()),
          },
          success: (res) => {
            if (res.isUpdate == 'N') {
              let that = this
              hllayer.confirm1('当前未检测到合同基本信息与乙方信息发生变动，请确认变更信息，或返回首页取消基本信息变更选项', function () {
                that.previous()
              })
            } else {
              if (window.changePage.steps.join(',').indexOf('1') == -1) {
                let cache = JSON.parse(window.sessionStorage.getItem('CHANGE_CONTRACT_INDEX'))
                var data = {
                  "alter_id": cache.alter_id,
                  "alter_no": cache.alter_no,
                  "alter_type": '',
                  "alert_description": cache.alert_description || '#',
                  "effective_date": cache.effective_date,
                  "contract_id": this.id,
                  "m_code": this.contractDetail.mCode || '#', // 原合同管理编号
                  "c_code": this.contractDetail.cCode || '#', //合同编号
                  "name": this.contractDetail.name || '#', //合同名称
                  "memo": this.contractDetail.memo || '#', //合同备注
                  "sign_time": this.contractDetail.sign_time || '#', // 签订时间
                  "contract_type": this.contractDetail.contractType || '#', //合同类型
                  "zlf_info": JSON.stringify({ //客户信息
                    zlf_type: this.contractDetail.zlfType + '',
                    zlf_id: this.contractDetail.zlfId + '' || '#',
                    zlf_name: this.contractDetail.zlfCompany || '#',
                    zlf_lperson: this.contractDetail.zlfLperson || '#',
                    zlf_sperson: this.contractDetail.zlfSperson || '#',
                    zlf_lphone: this.contractDetail.zlfLphone || '#',
                    zlf_sphone: this.contractDetail.zlfSphone || '#',
                    zlf_industry_type: this.contractDetail.zlfIndustryType + '' || '#',
                    zlf_address: this.contractDetail.zlfAddress || '#',
                    short_name: this.contractDetail.shortName || '#',
                    idcode: this.contractDetail.idcode || '#',
                    email: this.contractDetail.email || '#',
                    channel_category_code: this.contractDetail.channelCategoryCode + '' || '#',
                    channel_name_code: this.contractDetail.channelNameCode + '' || '#',
                    channel_name_value: this.contractDetail.channelNameVal || '#'
                  }),
                  "custom_config_property": JSON.stringify(this.contractDetail.ZDYdata),
                  page_customize_component_biz_content: JSON.stringify(this.newFieldsData()),

                  "project_info": '#',
                  "rent_info": '#',
                  "expense": '#',
                  "bill_plans": '#',
                  "commission_info": '#',
                  "alteration_settle_ups": '#',
                  "alteration_bill": '#',
                }
                arr = []
                if (cache.alter_type.indexOf('CW') != -1) {
                  arr.push('CW')
                }
                if (cache.alter_type.indexOf('YB') != -1) {
                  arr.push('YB')
                }
                data.alter_type = arr.length == 2 ? 'ALL' : arr + ''

                hlhttp.post({
                  url: 'contract_history/_contrast',
                  data: data,
                  success: (res) => {
                    let type = ['add', 'change', 'delete']
                    res.groupLogs.map(item => {
                      item.group = 'N'
                      item.logRecords.map(item1 => {
                        item1.rowData.map(item2 => {
                          if (type.indexOf(item2.logType) >= 0) {
                            item.group = 'Y'
                          }
                        })
                      })
                    })
                    window.sessionStorage.setItem('CHANGE_CONTRACT_GROUPLOPS', JSON.stringify(res.groupLogs))
                    window.sessionStorage.setItem('CONTRACT_DETAIL', JSON.stringify(this.contractDetail))
                    window.sessionStorage.setItem('CHANGE_CONTRACT_CONTRAST', JSON.stringify(data))
                    window.changePage.pageIndex++
                  }
                })
              } else {
                window.sessionStorage.setItem('CONTRACT_DETAIL', JSON.stringify(this.contractDetail))
                window.changePage.pageIndex++
              }
            }
          }
        })
      },
      // 上一步/下一步
      previous() {
        window.sessionStorage.setItem('CONTRACT_DETAIL', JSON.stringify(this.contractDetail))
        window.changePage.pageIndex--
      },
      next() {
        if (!this.checkCustom()) {
          hl.toast('请完成自定义字段')
          return
        }

        if (!this.checkComplete()) return
        this.checkChange()
      }
    },
    mounted() {
      this.getIndustry()
      this.getChannelType()
      this.contractDetail = JSON.parse(window.sessionStorage.getItem('CONTRACT_DETAIL'))
    },
  })
}())